package com.cskaoyan.controller.wx.comment;

import com.cskaoyan.bean.MarketComment;
import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.bo.wx.comment.CommentOfListBo;
import com.cskaoyan.bean.bo.wx.comment.CommentOfPostBo;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.vo.wx.comment.CommentForcountVo;
import com.cskaoyan.bean.vo.wx.comment.CommentListEntityVo;
import com.cskaoyan.bean.vo.wx.comment.CommentOfPostVo;
import com.cskaoyan.service.wx.comment.CommentsService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Mr.Tang
 * @since 2022/09/11 16:19
 */
@RestController
@RequestMapping("wx/comment")
public class CommentsController {
    @Autowired
    CommentsService commentsService;

    /**
     * 评论清单
     * Request URL: http://localhost:8083/wx/comment/list?
     * valueId=1116011&type=0&limit=20&page=1&showType=0
     * @return
     */
    @RequestMapping("list")
    public BaseRespVo listForComment(CommentOfListBo commentOfListBo) {

        CommonData<CommentListEntityVo> commentForList = commentsService.listForComments(commentOfListBo);

        return BaseRespVo.ok(commentForList);
    }

    /**
     * 评论条数
     * Request URL: http://182.92.235.201:8083/wx/comment/count?valueId=1057036&type=0
     * @param valueId
     * @param type
     * @return
     */
    @RequestMapping("count")
    public BaseRespVo countForComment(Integer valueId, Byte type) {
       CommentForcountVo conmentForcountVo = commentsService.countForcomment(valueId,type);

        return BaseRespVo.ok(conmentForcountVo);
    }


    /**
     * 发表评论
     * {type: 1, valueId: "373", content: "实用", star: 5, hasPicture: false, picUrls: []}
     * @param
     * @return
     */
    @RequestMapping("post")
    public BaseRespVo postForComment(@RequestBody MarketComment marketComment) {
        // 获取当前用户id
        Subject subject = SecurityUtils.getSubject();
        MarketUser primaryPrincipal =(MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer uid = primaryPrincipal.getId();
        List<MarketComment> commentOfPostVo = commentsService.postForComment(marketComment,uid);
        return BaseRespVo.ok(commentOfPostVo);
    }

}
